import 'package:easy_refresh/easy_refresh.dart';
import 'package:pornhub_app/components/base_refresh/base_refresh_page_counter.dart';
import 'package:pornhub_app/components/base_refresh/base_refresh_controller.dart';
import 'package:pornhub_app/components/customize_alert.dart';
import 'package:pornhub_app/components/easy_toast.dart';
import 'package:pornhub_app/http/api/api.dart';
import 'package:pornhub_app/model/video_base_model.dart';
import 'package:get/get.dart';

const _pageSize = 10;

class BuyVideoPageController extends BaseRefreshController
    with BaseRefreshPageCounter {
  final videos = <VideoBaseModel>[].obs;
  var visible = false.obs;
  var allSelect = false.obs;

  @override
  Future<IndicatorResult> onRefresh() async {
    onResetRefresh();
    resetPage();
    final resp = await Api.getUserPurVideo(
      page: page,
      pageSize: _pageSize,
      videoMark: 1,
    );
    if (resp == null) {
      return IndicatorResult.fail;
    }
    incPage();
    _setList(resp);
    return IndicatorResult.success;
  }

  @override
  Future<IndicatorResult> onLoad() async {
    onResetRefresh();
    final resp = await Api.getUserPurVideo(
      page: page,
      pageSize: _pageSize,
      videoMark: 1,
    );
    if (resp == null) {
      return IndicatorResult.fail;
    }

    _addList(resp);
    if (resp.length < _pageSize) {
      return IndicatorResult.noMore;
    }

    incPage();
    return IndicatorResult.success;
  }

  void onSelectedItem(int index) async {
    if (videos.isNotEmpty) {
      videos[index].isSelected = !videos[index].isSelected!;
      var list = <VideoBaseModel>[];
      list.addAll(videos);
      videos.clear();
      videos.addAll(list);
    }
  }

  void onAllSelected() async {
    if (videos.isNotEmpty) {
      for (var i = 0; i < videos.length; i++) {
        videos[i].isSelected = allSelect.value;
      }
      var list = <VideoBaseModel>[];
      list.addAll(videos);
      videos.clear();
      videos.addAll(list);
    }
  }

  void onResetItem() async {
    allSelect.value = false;
    onAllSelected();
  }

  void onResetRefresh() {
    visible.value = false;
    onResetItem();
  }

  void onDeleteItem() async {
    if (videos.isNotEmpty) {
      List<int> ids = [];
      var list = <VideoBaseModel>[];
      for (var i = 0; i < videos.length; i++) {
        if (videos[i].isSelected == true) {
          ids.add(videos[i].videoId!);
        } else {
          list.add(videos[i]);
        }
      }
      if (ids.isEmpty) {
        EasyToast.show('请勾选视频');
        return;
      }
      CustomizeAlert(Get.context!, title: '确认删除所选视频', submit: '确认',
          okAction: () async {
        final resp = await Api.clearUserRecord(
          type: 3,
          videoIds: ids,
        );
        if (resp) {
          EasyToast.show('删除成功');
          videos.clear();
          videos.addAll(list);
          onResetRefresh();
        }
      });
    }
  }

  void _setList(List<VideoBaseModel>? list) {
    videos.clear();
    _addList(list);
  }

  void _addList(List<VideoBaseModel>? list) {
    if (null != list && list.isNotEmpty) {
      videos.addAll(list);
    }
  }
}
